import { listTemplate } from '@/api/manage/template';
// import { listClassify } from './../../../manage/classify';
import { computed, ref, watch } from 'vue';
export default (form:any)=>{
	// const classifyList = ref<any>('');
	const templateList = ref<any>('');
	const filteredOptions = ref<any>([]);
	// 此区域根据选择模版不同现实表单不同
	const selectShowList:{label:string,prop:string,placeholder:string,type:string}[] = [
		{label:'储藏方式',prop:'saveMethodContent',placeholder:'储藏方式', type:'selectAndInput'},
		// {label:'效期类型',prop:'expiryType',placeholder:'效期类型', type:'select'},
		// {label:'存储时间',prop:'qualityPeriod',placeholder:'保质期', type:'number'},
		// {label:'时间类型',prop:'dateType',placeholder:'保质期', type:'select'},
		{label:'解冻时间文案',prop:'thawTimeWord',placeholder:'解冻时间', type:'text'},
		{label:'解冻时间(小时)',prop:'thawTime',placeholder:'解冻时间', type:'number'},
		{label:'开始时间文案',prop:'begintimeWord',placeholder:'打印时间', type:'text'},
		{label:'结束时间文案',prop:'endtimeWord',placeholder:'过期时间', type:'text'},
		{label:'制作人文案',prop:'producerWord',placeholder:'制作人', type:'text'},
		{label:'规格文案',prop:'specsWord',placeholder:'规格', type:'text'},
		{label:'规格内容',prop:'pecsContent',placeholder:'规格内容', type:'text'},
		{label:'主要配料文案',prop:'batchingWord',placeholder:'主要配料', type:'text'},
		{label:'主要配料内容',prop:'batchingContent',placeholder:'主要配料内容', type:'text'},
		{label:'二维码内容',prop:'qrcodeContent',placeholder:'二维码内容', type:'text'},
		{label:'条形码内容',prop:'bccodeContent',placeholder:'条形码内容', type:'text'},
		{label:'自定义文案1',prop:'customWordOne',placeholder:'自定义文案1', type:'text'},
		{label:'自定义文案2',prop:'customWordTwo',placeholder:'自定义文案2', type:'text'},
	];

	async function init(){
		
		// const res:any = await listClassify();
		const tempRes:any = await listTemplate();
		// classifyList.value = res.data.list;
		// console.log('查询所有产品库分类:',res,'所有模版:',tempRes);
		templateList.value = tempRes.data.list;
	}
	init();

	/**
	 * 判读传入的值能不能转成数字
	 * @param str 传入值
	 * @returns 返回数字或者字符串
	 */
	function convertToNumber(str: string): number | string {
		// 判断字符串是否可以转换为数字
		if (!isNaN(parseFloat(str))) {
		  return parseFloat(str); // 如果可以转换，使用parseFloat转换为数字
		} else {
		  return str; // 如果不是数字类型的字符串，直接返回原字符串
		}
	  }
	// watch(()=>form.value.templateId,(newVal)=>{
	// 	filteredOptions.value = [];
	// 	if(!newVal) return;
	// 	const filterVal = templateList.value.filter((ele:any)=>ele.id === newVal);
	// 	const selectTemplate = filterVal[0] as any;
	// 	const templateContent = JSON.parse(selectTemplate.templateContent); 
	// 	templateContent.elements.forEach((ele:any) => {
	// 		const cpList = Object.keys(ele).filter(str=> str.includes('_'));
	// 		cpList.forEach(property => {
	// 			const params = property.split('_')[1]; // 获取单词
	// 			const res = selectShowList.filter(item=>item.prop===params);
	// 			if(res.length){
	// 				filteredOptions.value = [...filteredOptions.value,...res];
	// 				form.value[params] = convertToNumber(ele[property]);
	// 			}
	// 			// 根据日期类型设置
	// 			if(params === 'startTime'){
	// 				form.value.dateFormat = ele[property];
	// 			}
	// 		  });
	// 	});
	// });
	/**选中的模版类型 */
	const filterTemplateType = ref<any>('');
	const changeTemplate = (val:string,origin?:'change')=>{
		filteredOptions.value = [];
		filterTemplateType.value = '';
		if(!val) return;
		const filterVal = templateList.value.filter((ele:any)=>ele.id === val);
		const selectTemplate = filterVal[0] as any;
		filterTemplateType.value = selectTemplate.templateType;
		const templateContent = JSON.parse(selectTemplate.templateContent); 
		templateContent.elements.forEach((ele:any) => {
			const cpList = Object.keys(ele).filter(str=> str.includes('_'));
			cpList.forEach(property => {
				const params = property.split('_')[1]; // 获取单词
				const res = selectShowList.filter(item=>item.prop===params);
				console.log(res,'---');
				if(res.length){
					filteredOptions.value = [...filteredOptions.value,...res];
					if(origin==='change'){
						form.value[params] = convertToNumber(ele[property]);
					}
					if(res[0].prop === 'thawTimeWord'){
						filteredOptions.value = [...filteredOptions.value,{label:'解冻时间(小时)',prop:'thawTime',placeholder:'解冻时间', type:'number'}];
					}
				}
				// 根据日期类型设置
				if(params === 'startTime' && origin==='change'){
					form.value.dateFormat = ele[property];
				}
			  });
		});
	};
	return {
		// classifyList,
		templateList,
		selectShowList,
		filteredOptions,
		filterTemplateType,
		changeTemplate
	};
};